package xyz.scootaloo.mono.admin.dto

import xyz.scootaloo.mono.base.util.EncryptUtil
import xyz.scootaloo.mono.data.entity.auth.AuthUser
import java.time.LocalDateTime

/**
 * @author flutterdash@qq.com
 * @since 2021/9/23 10:51
 */

/**
 * 在前端显示的用户信息表格, 每一行所显示内容
 */
data class SysUserVO(
    var id: Int,
    var account: String,
    var name: String,
    var email: String,
    var created: String,
    var lastLogin: String,
    var desc: String,
    var enable: Boolean
) {
    fun toAuthUser(): AuthUser {
        return AuthUser {
            uid = this@SysUserVO.id
            username = this@SysUserVO.account
            nickName = this@SysUserVO.name
            email = this@SysUserVO.email
            status = if (this@SysUserVO.enable)
                AuthUser.normal
            else
                AuthUser.disable
        }
    }

    companion object Converter {
        fun of(authUser: AuthUser): SysUserVO {
            return SysUserVO(
                id = authUser.uid,
                account = authUser.username,
                name = authUser.nickName,
                email = authUser.email,
                created = authUser.created.toString(),
                lastLogin = authUser.lastLogin.toString(),
                desc = authUser.desc,
                enable = authUser.status == AuthUser.normal
            )
        }
    }
}

/**
 * 快速创建账号所使用的数据结构
 */
data class QuickUserFormVO(
    var username: String,
    var password: String,
    var desc: String
) {
    fun fillAndToAuthUser(): AuthUser {
        val encoder = EncryptUtil.PasswordEncoder(password)
        return AuthUser.edit {
            username = this@QuickUserFormVO.username
            password = encoder.encoded
            salt = encoder.salt
            desc = this@QuickUserFormVO.desc
        }
    }
}

data class UpdateUserFormVO(
    var uid: Int,
    var username: String,
    var password: String,
    var nickname: String,
    var phone: String,
    var email: String,
    var sex: Int,
    var status: Int,
    var desc: String
) {
    fun toAuthUser(): AuthUser {
        val encoder = EncryptUtil.PasswordEncoder(this.password)
        return AuthUser {
            uid = this@UpdateUserFormVO.uid
            username = this@UpdateUserFormVO.username
            password = encoder.encoded
            salt = encoder.salt
            nickName = this@UpdateUserFormVO.nickname
            phone = this@UpdateUserFormVO.phone
            email = this@UpdateUserFormVO.email
            sex = this@UpdateUserFormVO.sex
            status = this@UpdateUserFormVO.status
            updated = LocalDateTime.now()
            desc = this@UpdateUserFormVO.desc
        }
    }
    companion object StaticMethods {
        fun of(authUser: AuthUser): UpdateUserFormVO {
            return UpdateUserFormVO(
                uid = authUser.uid,
                username = authUser.username,
                password = authUser.password,
                nickname = authUser.nickName,
                phone = authUser.phone,
                email = authUser.email,
                sex = authUser.sex,
                status = authUser.sex,
                desc = authUser.desc
            )
        }
    }
}
